﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data.Common;
    using System.Text;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class CycleCountTagBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private static readonly ICycleCountClassDA CycleCountClassDA = DataAccess.CreateCycleCountClassDA();
        private static readonly ICycleCountReleaseDA CycleCountReleaseDA = DataAccess.CreateCycleCountReleaseDA();
        private static readonly ICycleCountRequestDA CycleCountReuqestDA = DataAccess.CreateCycleCountRequestDA();
        private static readonly ICycleCountRuleDA CycleCountRuleDA = DataAccess.CreateCycleCountRuleDA();
        private static readonly ICycleCountTagDA CycleCountTagDA = DataAccess.CreateCycleCountTagDA();
        private List<string> dataBaseID;
        private static readonly IInvLotDA invLotDA = DataAccess.CreateInvLotDA();
        private static readonly IInvLotLocLpnDA InvLotLocLpnDA = DataAccess.CreateInvLotLocLpnDA();
        private static readonly IInvTransDA invTranDA = DataAccess.CreateInvTransDA();
        private static readonly ILotAttributeDA lotAttributeDA = DataAccess.CreateLotAttributeDA();
        private PubParasInfo pubParasInfo;
        private static readonly ISkuDA SkuDA = DataAccess.CreateSkuDA();

        public CycleCountTagBLL()
        {
            this.pubParasInfo = null;
        }

        public CycleCountTagBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public CycleCountTagBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteCycleCountTag(string releaseID, List<string> TagNo)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            string strTagNo = string.Empty;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            for (int i = 0; TagNo.Count > i; i++)
            {
                if (CycleCountTagDA.DeleteCycleCountTag(dataBase, tran, releaseID, TagNo[i], whLoginID) > 0)
                {
                    strTagNo = strTagNo + TagNo[i] + ";";
                }
            }
            return strTagNo;
        }

        public CycleCountTagInfo GetCycleCountTagByID(string ReleaseID, string TagNo)
        {
            return CycleCountTagDA.GetCycleCountTagByID(ReleaseID, TagNo, this.dataBaseID[0], this.pubParasInfo);
        }

        private void InsertCCAdjustmentTrans(DataBase dataBase, DbTransaction tran, CycleCountTagInfo cycleCountTag, string whLoginID)
        {
            InvTransInfo invTransInfo = new InvTransInfo {
                TransID = commonMethod.GenNextNumStr(dataBase, tran, "INV_TRANS", whLoginID),
                CaseCnt = 0M,
                Cube = 0.0,
                Descr = "",
                GrossWgt = 0.0,
                InnerPack = 0M,
                Lot = cycleCountTag.Lot,
                NetWgt = 0.0,
                OtherUnit1 = 0.0,
                OtherUnit2 = 0.0,
                OwnerID = cycleCountTag.OwnerID,
                PackID = "",
                Pallet = 0M,
                Qty = cycleCountTag.AdjQty,
                ReceiptID = "",
                ReceiptLineID = 0,
                DocID = cycleCountTag.ReleaseID,
                DocLineID = cycleCountTag.TagNo,
                SkuID = cycleCountTag.SkuID,
                Status = TransStatus.OK,
                TransType = TransType.Adjustment,
                UomID = "",
                SourceTransType = TransSourceType.CycleCountAdd,
                ReceiptDate = DateTime.Now,
                FromLoc = cycleCountTag.Loc,
                FromLpnID = cycleCountTag.LpnID,
                ToLoc = cycleCountTag.Loc,
                ToLpnID = cycleCountTag.LpnID
            };
            LotAttributeInfo lotAttributeInfo = lotAttributeDA.GetLotAttributeByID(dataBase, tran, cycleCountTag.Lot, whLoginID);
            invTransInfo.LotAttr01 = lotAttributeInfo.LotAttr01;
            invTransInfo.LotAttr02 = lotAttributeInfo.LotAttr02;
            invTransInfo.LotAttr03 = lotAttributeInfo.LotAttr03;
            invTransInfo.LotAttr04 = lotAttributeInfo.LotAttr04;
            invTransInfo.LotAttr05 = lotAttributeInfo.LotAttr05;
            invTransInfo.LotAttr06 = lotAttributeInfo.LotAttr06;
            invTransInfo.LotAttr07 = lotAttributeInfo.LotAttr07;
            invTransInfo.LotAttr08 = lotAttributeInfo.LotAttr08;
            invTransInfo.LotAttr09 = lotAttributeInfo.LotAttr09;
            invTransInfo.LotAttr10 = lotAttributeInfo.LotAttr10;
            invTransInfo.CreatedBy = cycleCountTag.UpdatedBy;
            invTransInfo.UpdatedBy = cycleCountTag.UpdatedBy;
            invTransInfo.TransDate = (cycleCountTag.CountDate != new DateTime()) ? cycleCountTag.CountDate : DateTime.Now;
            InvLotInfo invLotInfo = invLotDA.GetInvLotByID(dataBase, tran, cycleCountTag.Lot, whLoginID);
            if (invLotInfo != null)
            {
                invTransInfo.UnitPrice = invLotInfo.UnitPrice;
            }
            invTranDA.InsertInvTrans(dataBase, tran, invTransInfo, whLoginID);
        }

        public string UpdateCycleCountTag(List<CycleCountTagInfo> obj)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string releaseID = obj[0].ReleaseID;
            string strTagNo = string.Empty;
            try
            {
                List<string> RequestIDList = new List<string>();
                foreach (CycleCountTagInfo info in obj)
                {
                    if (!string.IsNullOrEmpty(info.RequestID) && !RequestIDList.Contains(info.RequestID))
                    {
                        RequestIDList.Add(info.RequestID);
                    }
                    if (info.SysQty != info.CountQty)
                    {
                        info.Status = this.UpdateSysQtyForCycleCount(dataBase, tran, info.OwnerID, info.SkuID, info.CountQty, info.SysQty, info.AdjQty, info.Lot, info.Loc, info.LpnID, whLoginID);
                    }
                    else
                    {
                        info.Status = CycleCountTagStatus.Posted;
                    }
                    info.UpdatedBy = this.pubParasInfo.UserID;
                    if (CycleCountTagDA.UpdateCycleCountTagQty(dataBase, tran, info, whLoginID) > 0)
                    {
                        strTagNo = strTagNo + info.TagNo.ToString() + ";";
                        this.InsertCCAdjustmentTrans(dataBase, tran, info, whLoginID);
                    }
                }
                string strStatus = string.Empty;
                for (int i = 0; RequestIDList.Count > i; i++)
                {
                    if (CycleCountTagDA.GetCycleCountRequestStatus(dataBase, tran, RequestIDList[i], CycleCountTagStatus.Posted, whLoginID) > 0)
                    {
                        strStatus = CycleRequestStatus.InCounting;
                    }
                    else
                    {
                        strStatus = CycleRequestStatus.Counted;
                    }
                    CycleCountReuqestDA.UpdateCycleCountRequestStatus(dataBase, tran, strStatus, RequestIDList[i], whLoginID);
                }
                if (CycleCountTagDA.GetCycleCountReleaseStatus(dataBase, tran, releaseID, CycleCountTagStatus.Posted, whLoginID) > 0)
                {
                    strStatus = CycleCountReleaseStatus.InCounting;
                }
                else
                {
                    strStatus = CycleCountReleaseStatus.Counted;
                }
                CycleCountReleaseDA.UpdateCycleCountReleaseStatus(dataBase, tran, releaseID, strStatus, whLoginID);
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (!string.IsNullOrEmpty(strTagNo))
            {
                strTagNo = strTagNo.Substring(0, strTagNo.Length - 1);
            }
            return strTagNo;
        }

        public string UpdateSysQtyForCycleCount(DataBase dataBase, DbTransaction tran, string OwnerID, string SkuID, decimal CountQty, decimal SysQty, decimal AdjQty, string Lot, string Loc, string LpnID, string whLoginID)
        {
            SkuInfo skuInfo = SkuDA.GetSkuByID(dataBase, tran, OwnerID, SkuID, whLoginID);
            if (!string.IsNullOrEmpty(skuInfo.Abc))
            {
                CycleCountClassInfo cycleCountClassInfo = CycleCountClassDA.GetCycleCountClassByID(dataBase, tran, skuInfo.Abc, whLoginID);
                AdjustmentBLL adjustbll = new AdjustmentBLL();
                if (cycleCountClassInfo != null)
                {
                    StringBuilder strOrderby = new StringBuilder();
                    if (AdjQty > 0M)
                    {
                        if (AdjQty <= (SysQty * (cycleCountClassInfo.PosQtyVariance / 100M)))
                        {
                            adjustbll.AdjustInventoryQty(dataBase, tran, Lot, Loc, LpnID, SkuID, OwnerID, this.pubParasInfo.UserID, AdjQty, whLoginID);
                            return CycleCountTagStatus.Posted;
                        }
                        return CycleCountTagStatus.OutOfVariance;
                    }
                    if (Math.Abs(AdjQty) <= (SysQty * (cycleCountClassInfo.PosQtyVariance / 100M)))
                    {
                        adjustbll.AdjustInventoryQty(dataBase, tran, Lot, Loc, LpnID, SkuID, OwnerID, this.pubParasInfo.UserID, AdjQty, whLoginID);
                        return CycleCountTagStatus.Posted;
                    }
                    return CycleCountTagStatus.OutOfVariance;
                }
                return CycleCountTagStatus.NotPosted;
            }
            return CycleCountTagStatus.NotPosted;
        }

        public string UpdateTagQtyByReleaseIDAndTagNo(CycleCountTagInfo cycleCountTag)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strTagNo = string.Empty;
            try
            {
                new AdjustmentBLL().AdjustInventoryQty(dataBase, tran, cycleCountTag.Lot, cycleCountTag.Loc, cycleCountTag.LpnID, cycleCountTag.SkuID, cycleCountTag.OwnerID, this.pubParasInfo.UserID, cycleCountTag.AdjQty, whLoginID);
                cycleCountTag.Status = CycleCountTagStatus.Posted;
                cycleCountTag.UpdatedBy = this.pubParasInfo.UserID;
                if (CycleCountTagDA.UpdateCycleCountTagQty(dataBase, tran, cycleCountTag, whLoginID) > 0)
                {
                }
                string strStatus = string.Empty;
                if (!string.IsNullOrEmpty(cycleCountTag.RequestID))
                {
                    if (CycleCountTagDA.GetCycleCountRequestStatus(dataBase, tran, cycleCountTag.RequestID, cycleCountTag.ReleaseID, CycleCountTagStatus.Posted, whLoginID) > 0)
                    {
                        strStatus = CycleRequestStatus.InCounting;
                    }
                    else
                    {
                        strStatus = CycleRequestStatus.Counted;
                    }
                    CycleCountReuqestDA.UpdateCycleCountRequestStatus(dataBase, tran, strStatus, cycleCountTag.RequestID, whLoginID);
                }
                if (CycleCountTagDA.GetCycleCountReleaseStatus(dataBase, tran, cycleCountTag.ReleaseID, CycleCountTagStatus.Posted, whLoginID) > 0)
                {
                    strStatus = CycleCountReleaseStatus.InCounting;
                }
                else
                {
                    strStatus = CycleCountReleaseStatus.Counted;
                }
                CycleCountReleaseDA.UpdateCycleCountReleaseStatus(dataBase, tran, cycleCountTag.ReleaseID, strStatus, whLoginID);
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strTagNo;
        }
    }
}

